package iscript.uiFramework.components.silder
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.geom.Rectangle;
	import iscript.uiFramework.components.button.ImageButton;
	import iscript.uiFramework.components.silder.SilderType;
	import iscript.uiFramework.UIFramework;
	import iscript.uiFramework.util.Scale9GridBitmap;
	import iscript.uiFramework.util.UIComponentUtil;
	
	/**
	 * Silder ui元素布局
	 *
	 */
	internal class AbstractSilderDesinger extends Sprite
	{
		private var _clingSprite9Grid:Scale9GridBitmap;
		private var _bottomSprite9Grid:Scale9GridBitmap;
		
		/**
		 * 背景图片容器
		 */
		public var bottomContainer:Sprite;
		/**
		 * 背景图片
		 */
		public var bottomBitmap:BitmapData;
		
		/**
		 * 粘着在背景上的图片
		 */
		public var clingBitmap:BitmapData;
		
		/**
		 * 进度的遮罩
		 */
		private var _clingSpriteMask:Bitmap;

		/**
		 * 滑块图片
		 */
		public var BlockBitmap:Bitmap;
		
		/**
		 * 中间滑块容器
		 */
		public var block:Sprite;
		
		/**
		 * 滑块是否包含在内部
		 */
		private var _inner:Boolean;
		private var _viewRectangle:Rectangle;
		private var _theme:String;
		
		public var  topButton:ImageButton;
		public var  bottomButton:ImageButton;
		
		public function AbstractSilderDesinger(theme:String, inner:Boolean, viewRectangle:Rectangle)
		{
			_viewRectangle = viewRectangle
			_theme = theme;
			_inner = inner;
			block = new Sprite();
			bottomContainer = new Sprite();
			addChild(bottomContainer);
			
			bottomBitmap = UIFramework.resource.getBitmapData("GameLite.UIComponent." + _theme + ".BaseSilder.Bottom");
			clingBitmap = UIFramework.resource.getBitmapData("GameLite.UIComponent." + _theme + ".BaseSilder.Cling");
			BlockBitmap = new Bitmap(UIFramework.resource.getBitmapData("GameLite.UIComponent." + _theme + ".BaseSilder.Block"));
		}
		
		//Doing  at  UIComponent  Initialize  
		internal function initialize():void
		{
			if (topButton != null && bottomButton != null) {
				  topButton.render(this);
				  bottomButton.render(this);
				}
			
			
			_bottomSprite9Grid = new Scale9GridBitmap(bottomBitmap, _viewRectangle);
			bottomContainer.addChild(_bottomSprite9Grid);
			
			_clingSprite9Grid = new Scale9GridBitmap(clingBitmap, _viewRectangle);
			_clingSprite9Grid.width = _bottomSprite9Grid.width;
			bottomContainer.addChild(_clingSprite9Grid);
			
			_clingSpriteMask = new Bitmap(new BitmapData(1, clingBitmap.height, false, 0x000000));
			_clingSprite9Grid.mask = _clingSpriteMask;
			bottomContainer.addChild(_clingSpriteMask);
			
			if (!_inner && block.numChildren == 0)
			{
				BlockBitmap.x = -BlockBitmap.width / 2;
				block.addChild(BlockBitmap);
			}
			block.y = (_bottomSprite9Grid.height - block.height) / 2;
			addChild(block);
			
			
			setWidth(100);
			if (topButton != null){
				block.x = topButton.width;
			}
			
		}
		
		/**
		 * 修改滑块
		 * @param	target
		 */
		internal function setBlock(target:DisplayObject):void
		{
			if (block.numChildren > 0)
				block.removeChildAt(0)
			UIComponentUtil.addChild(target, block);
		}
		
		/**
		 * 改变遮罩宽度
		 * @param	value
		 */
		internal function changeMask(value:Number):void
		{
			_clingSpriteMask.width = value;
		}
		
		/**
		 * 设置silder 宽度
		 * @param	value
		 */
		internal function setWidth(value:Number):void
		{
			//_objectWidth = value;
			
			if (topButton != null && bottomButton != null) {
				  value -= topButton.width;
				  value -= bottomButton.width;
				}
			_clingSprite9Grid.width = value;
			_bottomSprite9Grid.width = value;
			if (topButton != null) {
				   bottomContainer.x = topButton.width;
				}
			if (bottomButton != null) {
				  bottomButton.x = topButton.width +bottomContainer.width;
				}
		 
		}
	
	}

}
